﻿function initHistory(){ 
var query = { 'text': '' }; 
chrome.history.search(query, function(results) { 


        console.log(results); 


for(var i = 0; i < results.length ; i++){ 
if(i == 20){ 
break; 
} 
if(results[i].title.trim().length == 0){ 
continue; 
} 
$("#history").append("<li><a href='"+ results[i].url +"' target='_blank'>" + results[i].title +"</a><span>"+ stringToDateTime(new Date(results[i].lastVisitTime)) +"<span> </li>"); 
} 
}); 
} 
$(function(){ 
initHistory(); 
}); 
function stringToDateTime(postdate) 
{ 
    var second = 1000; 
    var minutes = second*60; 
    var hours = minutes*60; 
    var days = hours*24; 
    var months = days*30; 
    var twomonths = days*365; 
    var myDate = postdate; 
    if (isNaN(myDate)) 
    { 
    myDate =new Date(postdate.replace(/-/g, "/")); 
    } 
                            var nowtime = new Date(); 
                            var longtime =nowtime.getTime()- myDate.getTime(); 
                            var showtime = 0; 
                            if( longtime > months*2 ) 
{ 
    return postdate; 
    } 
                            else if (longtime > months) 
{ 
    return "1个月前"; 
    } 
                            else if (longtime > days*7) 
{ 
    return ("1周前"); 
    } 
                            else if (longtime > days) 
{ 
    return(Math.floor(longtime/days)+"天前"); 
    } 
                            else if ( longtime > hours) 
{ 
    return(Math.floor(longtime/hours)+"小时前"); 
    } 
                            else if (longtime > minutes) 
{ 
    return(Math.floor(longtime/minutes)+"分钟前"); 
    } 
                            else if (longtime > second) 
{ 
    return(Math.floor(longtime/second)+"秒前"); 
    }else 
{ 
    return(longtime+" error "); 
    } 
} 